﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace Common
{
    public static class ValidateControl
    {
        //kiem tra email
        public static bool IsEmail(string email)
        {
            if (email == string.Empty)
            { return true; }
            string MatchEmailPattern = @"^(([^<>()[\]\\.,;:\s@\""]+"
                        + @"(\.[^<>()[\]\\.,;:\s@\""]+)*)|(\"".+\""))@"
                        + @"((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
                        + @"\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+"
                        + @"[a-zA-Z]{2,}))$";
            if (email != null)
            {
                return Regex.IsMatch(email, MatchEmailPattern);
            }
            else
            {
                return false;
            }
        }

        public static bool IsNumber(string number)
        {
            bool kt = false;
            try
            {
                string MatchNumber = @"^[-+]?[0-9]*\.?[0-9]+$";
                if (number != null)
                {
                    kt= Regex.IsMatch(number, MatchNumber);
                }
                else
                {
                    kt= false;
                }
            }
            catch { kt = false; }
            return kt;
        }

        //string ko co ky tu dac biet
        public static bool IsString(string sString)
        {
            bool kt = false;
            try
            {
                string MatchString = "(^[A-Za-z])([A-Za-zo]*)([A-Za-zo]$)";
                if (sString != null)
                {
                    kt = Regex.IsMatch(sString, MatchString);
                }
                else
                {
                    kt = false;
                }
            }
            catch { kt = false; }
            return kt;
        }

        public static void CheckNumber(KeyPressEventArgs e)
        {
            try
            {
                switch (e.KeyChar)
                {
                    case '0':
                        e.Handled = true;
                        break;
                    case '1':
                        e.Handled = true;
                        break;
                    case '2':
                        e.Handled = true;
                        break;
                    case '3':
                        e.Handled = true;
                        break;
                    case '4':
                        e.Handled = true;
                        break;
                    case '5':
                        e.Handled = true;
                        break;
                    case '6':
                        e.Handled = true;
                        break;
                    case '7':
                        e.Handled = true;
                        break;
                    case '8':
                        e.Handled = true;
                        break;
                    case '9':
                        e.Handled = true;
                        break;
                }
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }

        public static void CheckString(KeyPressEventArgs e)
        {
            try
            {
                if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                {
                    e.Handled = true;
                }
            }
            catch (Exception)
            {}
        }
    }
}
